﻿using System;
using System.Collections;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Connection;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;
using Thrift;

namespace Me.Prettyprint.Cassandra.Model
{
    /// <summary>
    /// CqlRows is a subclass of OrderedRowsImpl and is meant to hold the results
    /// of a CqlQuery. The constructor converts the thrift objects to Hector
    /// objects.
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="N"></typeparam>
    /// <typeparam name="V"></typeparam>
    public class CqlRows<K, N, V> : OrderedRowsImpl<K, N, V>
    {
        public CqlRows(Dictionary<K, List<Column>> thriftRet,
            Serializer<N> nameSerializer, Serializer<V> valueSerializer) : base(thriftRet, nameSerializer, valueSerializer)
        {
            ResultType = CqlResultType.ROWS;
            if (Count == 1)
            {
                IEnumerator<Row<K, N, V>> enumerator = GetEnumerator();
                enumerator.MoveNext();
                Row<K, string, V> row = (Row<K, string, V>)enumerator.Current;
                HColumn<string,V> column = row.ColumnSlice.GetColumnByName("count");
                if (column != null)
                {
                    AsCount = (int)LongSerializer.Instance.FromBytes(column.ValueBytes);
                }
            }
        }
        public CqlRows() : base()
        {
            ResultType = CqlResultType.VOID;
        }
        protected CqlResultType ResultType
        {
            get;
            set;
        }
        public int GetAsCount()
        {
            return AsCount;
        }
    }
}
